package com.bishe.psy.service.impl;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.dao.LikeArticleMapper;
import com.bishe.psy.exception.ApplicationException;

import com.bishe.psy.model.LikeArticle;
import com.bishe.psy.service.IArticleService;
import com.bishe.psy.service.ILikeArticleService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-16
 * Time: 11:21
 */


@Slf4j
@Service
public class LikeArticleServiceImpl implements ILikeArticleService {

    @Resource
    private LikeArticleMapper likeArticleMapper;

    @Resource
    private IArticleService iArticleService;

    @Override
    public LikeArticle selectLikeById(Long articleId, Long userId, Long currentIdentity) {
        //1.参数校验
        if(articleId == null || articleId <= 0 || userId == null || userId <= 0
        || currentIdentity == null || currentIdentity <= 0 ) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        LikeArticle likeArticle = likeArticleMapper.selectLikeById(articleId, userId, currentIdentity);

        //3.返回数据
        return likeArticle;
    }


    @Override
    public void updateIsLikeState(Long id) {

        //1.参数校验
        if(id == null) {
            log.warn("点赞表的id为空");
            throw new ApplicationException(AppResult.failed(ResultCode.AILED_USER_EXISTS));
        }

        //2根据id查询点赞记录
        LikeArticle likeArticle = likeArticleMapper.selectByPrimaryKey(id);

        //3.参数校验
        if(likeArticle == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //4.根据id修改isLike状态
        boolean flag = true; // 取消点赞

        LikeArticle likeArticle1 = new LikeArticle();
        likeArticle1.setId(likeArticle.getId());
        if(likeArticle.getIsLike() == 1) {
            likeArticle1.setIsLike((byte) 0);
        } else {
             flag = false;
            likeArticle1.setIsLike((byte) 1);
        }
        int row = likeArticleMapper.updateByPrimaryKeySelective(likeArticle1);

        //5.参数校验
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

        if(flag == true) {
            iArticleService.subOneArticleLikeCountById(likeArticle.getArticleId());
        } else {
            iArticleService.addOneArticleLikeCountById(likeArticle.getArticleId());
        }

        log.info("isLike状态更新成功");
    }

    @Override
    public void addOneLikeLogs(LikeArticle likeArticle) {
        //1.参数校验
        if(likeArticle == null || likeArticle.getUserId() == null || likeArticle.getUserId() <= 0
                || likeArticle.getArticleId() == null || likeArticle.getArticleId() <= 0
                || likeArticle.getCurrentIdentity() == null || likeArticle.getCurrentIdentity() <=0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.参数校验通过，新增数据
        likeArticle.setIsLike((byte) 1);
        likeArticle.setDeleteState((byte) 0);
        Date date = new Date();
        likeArticle.setCreateState(date);
        likeArticle.setUpdateState(date);


        //3.数据插入到点赞表中
        int row = likeArticleMapper.insert(likeArticle);//动态插入信息
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

        //4.点赞记录插入成功，更新文章表的likeCount数量
        iArticleService.addOneArticleLikeCountById(likeArticle.getArticleId());

        //.走在这里，说明两个sql语句都成功，有一个sql语句失败都不能够走到这一行
        log.info("点赞记录插入成功，并且文章点赞数量+1");
    }

}
